/**
 * 
 */
package edu.scsu.csci.view;

import java.util.HashMap;
import java.util.Map;

import edu.scsu.csci.IView;
import edu.scsu.csci.core.IRegistry;


/**
 *
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
public class ViewRegistry implements IRegistry<Class<? extends IView>, IView> {
		
	/**
	 * mapping information for view class and the respective singleton objects.
	 */
	private Map<Class<? extends IView>, IView> viewMapping;
	
	/**
	 * singleton.
	 */
	private static ViewRegistry instance;
	
	/**
	 * Returns the singleton registory instance.
	 * 
	 * @return
	 */
	public synchronized static ViewRegistry getInstance(){
		if(instance == null){
			instance = new  ViewRegistry();
			instance.setUp();
		}
		return instance;
	}
	
	/**
	 * Creates the view registory.
	 */
	private ViewRegistry(){
		this.viewMapping = new HashMap<Class<? extends IView>, IView> ();
	}
		
	/**
	 * Registers the <code>view</code> to be used for the <code>viewClass</code>.
	 * 
	 * @param type			the type of the object which needs to be validated.
	 * @param validator		the validator object.
	 * @param annotation	the validator annotaion defining the validation rules.
	 */
	public void register(Class<? extends IView> viewClass, IView view){
		if(viewClass == null || view == null){
			throw new IllegalArgumentException("Neither of the parameters can be null. view: " + view 
						+ "\t viewClass: " + viewClass);
		}
		this.viewMapping.put(viewClass, view);
	}
	
	/**
	 * @param type
	 * @return
	 */
	public IView get(Class<? extends IView> viewClass){
		if(viewClass == null){
			throw new IllegalArgumentException("viewClass: " + viewClass + " cannot be null.");
		}
		IView validator = viewMapping.get(viewClass);
		if(validator == null){
			viewMapping.get(JSPView.class);
		}
		return validator;
	}
	
	/**
	 * Registers all the basic convertors.
	 */
	private void setUp(){
		this.register(JSPView.class, new JSPView());
		this.register(VelocityView.class, new VelocityView());
		this.register(XMLView.class, new XMLView());
		this.register(XSLTView.class, new XSLTView());
	}

}
